package com.qdlc.p2p.biz.task;

import org.apache.log4j.Logger;

import com.lmax.disruptor.EventHandler;
import com.qdlc.p2p.biz.service.AutoBorrowService;
import com.qdlc.p2p.biz.service.BorrowTenderService;
import com.qdlc.p2p.biz.service.borrow.BorrowFullVerifyService;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.BeanUtil;

public class CEventHandler implements EventHandler<ValueEvent> {
	Logger logger = Logger.getLogger(CEventHandler.class);

	@SuppressWarnings("unchecked")
	@Override
	public void onEvent(ValueEvent event, long sequence, boolean endOfBatch) throws Exception {
	    String result = "success";
        boolean isSystemMessage = false;
        AutoBorrowService autoBorrowService = (AutoBorrowService) BeanUtil.getBean("autoBorrowService");
		BorrowFullVerifyService borrowFullVerifyService = (BorrowFullVerifyService) BeanUtil.getBean("borrowFullVerifyService");
	    try {
			BorrowTenderService borrowTenderService = (BorrowTenderService)BeanUtil.getBean("borrowTenderService");
			
			//投标
			if("tender".equals(event.getOperate())){
				 isSystemMessage = true;
				 borrowTenderService.addTender(event.getBorrow(),event.getBorrowModel());
			// 还款
			}else if ("repay".equals(event.getOperate())) {
			    isSystemMessage = true;
		    	autoBorrowService.autoBorrowRepay(event.getBorrowRepayment());
			// 自动投标
			} else if ("autoTender".equals(event.getOperate())) {
			    isSystemMessage = true;
				autoBorrowService.autoDealTender(event.getBorrowModel());
			// 满标复审
			} else if ("autoVerifyFull".equals(event.getOperate())) {
                isSystemMessage = true;
                autoBorrowService.autoVerifyFull(event.getBorrowModel());
            // 复审通过
            } else if ("autoVerifyFullSuccess".equals(event.getOperate())) {
			    isSystemMessage = true;
//				borrowFullVerifyService.processVerifyFullSuccess(event.getBorrowModel());
			// 复审不通过
			} else if ("autoVerifyFullFail".equals(event.getOperate())) {
			    isSystemMessage = true;
//				borrowFullVerifyService.processVerifyFullFail(event.getBorrowModel());
			//管理员撤标
			} else if ("autoCancel".equals(event.getOperate())) {
			    isSystemMessage = true;
		    	autoBorrowService.autoCancel(event.getBorrow());
			} else if ("verifyCashBack".equals(event.getOperate())) {
			    isSystemMessage = true;
				//提现回调全部处理
				//accountCashService.cashCallBack(event.getCashModel());
			} else if ("verifyRecharge".equals(event.getOperate())) {
			    isSystemMessage = true;
				//充值回调全部处理
				//accountService.doRechargeTask(event.getRechargeModel());
			} else if ("doPriorRepay".equals(event.getOperate())) {
			    isSystemMessage = true;
				//提前还款
				autoBorrowService.doPriorRepay(event.getBorrowRepayment());
			}  else if ("overdue".equals(event.getOperate())) {
			    isSystemMessage = true;
				//网站垫付
				autoBorrowService.webAdvance(event.getBorrowRepayment());
			}  else if ("overduePayment".equals(event.getOperate())) {
			    isSystemMessage = true;
				//前台逾期垫付
				autoBorrowService.overduePayment(event.getBorrowRepayment());
			} else if ("doAddBorrow".equals(event.getOperate())) {
			    isSystemMessage = true;
			} else if ("doAddTender".equals(event.getOperate())) {
			    isSystemMessage = true;
			} else if ("doCompensateSuccess".equals(event.getOperate())) {
                isSystemMessage = true;
                //环讯代偿回调处理
                autoBorrowService.autoCompensateSuccess(event.getBorrowRepayment());
            }
		} catch (Exception e) {
			logger.info("exception="+e.getMessage());
			if(e instanceof com.qdlc.p2p.common.exception.BusinessException ){
                result = e.getMessage();
            }else{
                result = "系统异常，操作失败！！！";
            }	
			if("autoVerifyFullSuccess".equals(event.getOperate())){ //复审处理失败状态改为1 重新复审
				autoBorrowService.updateStatus(event.getBorrowModel().getId(), 1, 3);
			}
		}
	    if(isSystemMessage && event.getResultFlag()!=null){//在需要保存系统处理信息的地方直接保存进来
            Global.TASK_RESULT_MAP.put(event.getResultFlag(), result);
        }
	}
}
